﻿using JNPF.Basics.Models;
using JNPF.Basics.Models.WorkFlowForm.Dtos.ApplyDeliverGoods;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Transactions;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 发货申请单
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-07-10 
    /// </summary>
    public class ApplyDeliverGoodsBll
    {
        private ApplyDeliverGoodsIService service = new ApplyDeliverGoodsService();
        private FlowTaskBll flowTaskBll = new FlowTaskBll();

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<List<ApplyDeliverGoodsEntryEntity>> GetDeliverEntryList(string id)
        {
            try
            {
                return await service.GetDeliverEntryList(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<ApplyDeliverGoodsEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <param name="applyDeliverGoodsEntryEntityList">子表</param>
        public async Task Save(string id, ApplyDeliverGoodsEntity entity, List<ApplyDeliverGoodsEntryEntity> applyDeliverGoodsEntryEntityList,int type=0)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    #region 表单信息
                    if (string.IsNullOrEmpty(id))
                    {
                        entity.Id = CommonHelper.Guid;
                        foreach (var item in applyDeliverGoodsEntryEntityList)
                        {
                            item.Id = CommonHelper.Guid;
                            item.InvoiceId = entity.Id;
                            item.SortCode = applyDeliverGoodsEntryEntityList.IndexOf(item);
                        }
                        await service.Create(entity, applyDeliverGoodsEntryEntityList);
                        BillRuleBll.UseBillNumber("WF_ApplyDeliverGoodsNo");
                    }
                    else
                    {
                        entity.Id = id;
                        foreach (var item in applyDeliverGoodsEntryEntityList)
                        {
                            item.Id = CommonHelper.Guid;
                            item.InvoiceId = entity.Id;
                            item.SortCode = applyDeliverGoodsEntryEntityList.IndexOf(item);
                        }
                        await service.Update(entity, applyDeliverGoodsEntryEntityList);
                    }
                    #endregion

                    #region 流程信息
                    await flowTaskBll.Save(id, entity.FlowId, entity.Id, entity.FlowTitle, entity.FlowUrgent, entity.BillNo,type);
                    #endregion

                    scope.Complete();
                }
            }
            catch (TransactionAbortedException ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <param name="applyDeliverGoodsEntryEntityList">子表</param>
        public async Task Submit(string id, ApplyDeliverGoodsEntity entity, List<ApplyDeliverGoodsEntryEntity> applyDeliverGoodsEntryEntityList)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    #region 表单信息
                    if (string.IsNullOrEmpty(id))
                    {
                        entity.Id = CommonHelper.Guid;
                        foreach (var item in applyDeliverGoodsEntryEntityList)
                        {
                            item.Id = CommonHelper.Guid;
                            item.InvoiceId = entity.Id;
                            item.SortCode = applyDeliverGoodsEntryEntityList.IndexOf(item);
                        }
                        await service.Create(entity, applyDeliverGoodsEntryEntityList);
                        BillRuleBll.UseBillNumber("WF_ApplyDeliverGoodsNo");
                    }
                    else
                    {
                        entity.Id = id;
                        foreach (var item in applyDeliverGoodsEntryEntityList)
                        {
                            item.Id = CommonHelper.Guid;
                            item.InvoiceId = entity.Id;
                            item.SortCode = applyDeliverGoodsEntryEntityList.IndexOf(item);
                        }
                        await service.Update(entity, applyDeliverGoodsEntryEntityList);
                    }
                    #endregion

                    #region 流程信息
                    await flowTaskBll.Submit(id, entity.FlowId, entity.Id, entity.FlowTitle, entity.FlowUrgent, entity.BillNo, entity.MapTo<ApplyDeliverGoodsUpInput>());
                    #endregion

                    scope.Complete();
                }
            }
            catch (TransactionAbortedException ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 审核修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task Save(string id, Dictionary<string, object> dic)
        {
            try
            {
                var input = dic.ToObject<ApplyDeliverGoodsUpInput>();
                var entity = input.MapTo<ApplyDeliverGoodsEntity>();
                var entityList = input.entryList.MapToList<ApplyDeliverGoodsEntryEntity>();
                await this.Save(id,entity,entityList,1);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}
